package huriezy.quickcontacts.activities.adapters;

import huriezy.quickcontacts.R;
import huriezy.quickcontacts.activities.fragments.FacebookPickerFragment;
import huriezy.quickcontacts.models.LogDebug;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UserPickerAdapter extends BaseAdapter {

	private final List<Bundle>	usersInfos			= new ArrayList<Bundle>();

	private final int			rowLayout			= R.layout.com_facebook_picker_list_row;
	private final int			pictureViewStubId	= R.id.com_facebook_picker_profile_pic_stub;
	private final int			titleId				= R.id.com_facebook_picker_title;

	private LayoutInflater		inflater;

	public UserPickerAdapter(Activity parentActivity) {
		if (parentActivity != null) {
			inflater = parentActivity.getLayoutInflater();
		}
	}

	public void addUser(Bundle user) {
		if (user != null) {
			usersInfos.add(user);
		}
	}

	public void addAllUsers(List<Bundle> listUsers) {
		if (listUsers != null) {
			usersInfos.addAll(listUsers);
		}
	}

	public void clearUsers() {
		usersInfos.clear();
	}

	@Override
	public int getCount() {
		return usersInfos.size();
	}

	@Override
	public Object getItem(int position) {
		Bundle user = usersInfos.get(position);
		return user;
	}

	@Override
	public long getItemId(int position) {
		long result = -1;
		Bundle user = usersInfos.get(position);
		if (user != null) {
			String uid = (String) user.get(FacebookPickerFragment.UID_FIELD);
			if (uid != null) {
				result = uid.hashCode();
			}
		}
		return result;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			if (inflater != null) {
				convertView = inflater.inflate(rowLayout, null);

				holder = new ViewHolder();
				ViewStub viewStub = (ViewStub) convertView.findViewById(pictureViewStubId);
				ImageView imageView = (ImageView) viewStub.inflate();
				holder.pictureView = imageView;
				holder.titleView = (TextView) convertView.findViewById(titleId);

				convertView.setTag(holder);
			}
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}

		Bundle user = (Bundle) getItem(position);
		Bitmap pictureBitmap = null;
		if (user != null) {
			pictureBitmap = user.getParcelable(FacebookPickerFragment.PICTURE_BITMAP_FIELD);
			String name = user.getString(FacebookPickerFragment.NAME_FIELD);

			// Set values to this row
			if (pictureBitmap != null) {
				holder.pictureView.setImageBitmap(pictureBitmap);
			}
			else {
				LogDebug.info("Cannot retrieve image view", getClass());
			}

			if (name != null) {
				holder.titleView.setText(name);
			}
		}

		return convertView;
	}

	public class ViewHolder {
		public ImageView	pictureView;
		public TextView		titleView;
	}
}
